package dao;
import java.util.List;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import util.HibernateUtil;
import entity.Cliente;
import entity.Venda;

public class VendaDaoImplementation implements VendaDao {

	public void save(Venda venda) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(venda);
		transaction.commit();
		session.close();
	}

	public void update(Venda venda) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.update(venda);
		transaction.commit();
		session.close();
	}

	public void delete(Venda venda) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.delete(venda);
		transaction.commit();
		session.close();
	}

	public void consult(Venda venda) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		venda = (Venda) session.load(Venda.class, new String("Venda"));
	}

	public Venda getVenda(long id) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (Venda) session.load(Venda.class, id);
	}

	@Override
	public List<Venda> list() {
		Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        String sql = "SELECT new Venda( v.idVenda, v.nota, v.serie, v.totalNota, v.cliente, v.dataEmissao, v.dataChegada) FROM Venda v";
        List<Venda> lista = session.createQuery(sql).list();
        transaction.commit();
        session.close();
        return lista;
	}
	
	

}
